import os
import tornado
from whirlwind.db.mongo import Mongo
from tornado.web import authenticated
from tornado.web import asynchronous
from tornado.httpclient import AsyncHTTPClient
from tornado.locale import CSVLocale
from tornado.options import options
from whirlwind.view.decorators import route
from pytz import all_timezones
from HandlerExtension.BasicHandler import BasicHandler
from application.models.user import User
from TweetAPIAsync.SinaTweetAPI import SinaTweetAPI


@route('/api/sina/auth')
class SinaOAuthHandler(BasicHandler):
    @authenticated
    @asynchronous
    def get(self):
        user = self.get_current_user()
        self.nonactive_behavior(user)

        self.tweetapi = SinaTweetAPI()
        callback_url = options.domain + ':' options.port + '/profile/add_api'
        self.apimethod = self.tweetapi.get_request_token(oauth_callback=callback_url)
        http = AsyncHTTPClient()
        http_url, http_method, http_body = \
                SinaTweetAPI.build_http_request(self.apimethod)
        http.fetch(http_url, http_method, http_body, callback=_on_request_token)

    def _on_request_token(self, response):
        print response
        pass
        
